#include <QtGui> 
#include "myqtapp.h"
#include <stdlib.h> 
 
myQtApp::myQtApp(QWidget *parent)
{
    setupUi(this); // this sets up GUI
    connect( pushButton_scan, SIGNAL( clicked() ), this, SLOT( scan() ) ); 
    connect( pushButton_parse, SIGNAL( clicked() ), this, SLOT( parse() ) ); 

 
}
 
 
void myQtApp::scan()
{
  saveFile("sample.tny");
  system("./compiler sample.tny > result");
  loadFile("result");
}
 
 
void myQtApp::parse()
{
  saveFile("sample.tny");
  system("./compiler sample.tny 1 1 > result");
  loadFile("result");
}
 
 
void myQtApp::clear()
{
  
}
 
 
void myQtApp::about() 
{
}

void myQtApp::loadFile(const QString &fileName)
{
     QFile file(fileName);
     if (!file.open(QFile::ReadOnly | QFile::Text)) {
         QMessageBox::warning(this, tr("Application"),
                              tr("Cannot read file %1:\n%2.")
                              .arg(fileName)
                              .arg(file.errorString()));
         return;
     }

     QTextStream in(&file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
     plainTextEdit_out->setPlainText(in.readAll());
     QApplication::restoreOverrideCursor();

}
bool myQtApp::saveFile(const QString &fileName)
{
     QFile file(fileName);
     if (!file.open(QFile::WriteOnly | QFile::Text)) {
         QMessageBox::warning(this, tr("Application"),
                              tr("Cannot write file %1:\n%2.")
                              .arg(fileName)
                              .arg(file.errorString()));
         return false;
     }

     QTextStream out(&file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
     out <<plainTextEdit_source->toPlainText();
     QApplication::restoreOverrideCursor();
     return true;
}
